<?php

include("global.php");

$pagetitle=" - Administration - Themes";
$links=" > Administration > Themes";

include ("header.php");

include ("admin_header.php");

if($userloggedinarray[15]!=="administrator"){
   unset($tplcontent);
   $tplcontent['notice'].="Must be logged in as administrator to use control panel!";
   echo template('notice',$tplcontent);
}else{

@chmod("themes",octdec(777));
@chmod("templates",octdec(777));
@chmod("replacements",octdec(777));

if(!$editfile&&!$editfile2&&!$deletefolder&&!$copyfolder&&!$copyfolder2&&!$renamefolder&&!$renamefolder2&&!$filecontents){
unset($tplcontent);

$tplcontent['notice'].="
If you are unable to edit these files, make sure themes/templates/replacement directories have 777 chmod permissions<br><br>
NOTE: If you only have one theme and you rename it, the board will initially not find the theme and appear unformated until you goto another page<br><br>
Be sure to edit the theme option file before you rename a corresponding element so that everything will work together<br>";

for($n=0;$n<count($themesarray);$n++){
 @chmod("themes/$themesarray[$n]",octdec(777));
 $themelanguages=listdirs("themes/$themesarray[$n]");
 $tplcontent['themes'].="&nbsp;&nbsp;";
 $tplcontent['themes'].="$themesarray[$n]<br>";
 $tplcontent['themes'].="&nbsp;&nbsp;&nbsp;&nbsp;";
 $tplcontent['themes'].="<a href=\"admin_themes.php?editfile=themes/$themesarray[$n]/config.php\">Options</a><br>";
 
 for($p=0;$p<count($themelanguages);$p++){
  $tplcontent['themes'].="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $tplcontent['themes'].="$themelanguages[$p]";
  $tplcontent['themes'].=" | <a href=\"admin_themes.php?deletefolder=themes/$themesarray[$n]/$themelanguages[$p]\">Delete theme</a>";
  $tplcontent['themes'].=" | <a href=\"admin_themes.php?copyfolder=themes/$themesarray[$n]/$themelanguages[$p]\">Copy theme</a>";
  $tplcontent['themes'].=" | <a href=\"admin_themes.php?renamefolder=themes/$themesarray[$n]/$themelanguages[$p]\">Rename theme</a><br>";
  $tplcontent['themes'].="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $tplcontent['themes'].="<a href=\"admin_themes.php?editfile=themes/${themesarray[$n]}/$themelanguages[$p]/style.css\">CSS</a><br>";
  $tplcontent['themes'].="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $tplcontent['themes'].="<a href=\"admin_themes.php?editfile=themes/$themesarray[$n]/$themelanguages[$p]/functions.php\">Functions</a><br>";
 }
 
}

$templatesetarray=listdirs("templates");

for($n=0;$n<count($templatesetarray);$n++){
 @chmod("templates/$templatesetarray[$n]",octdec(777));
 $tplcontent['templates'].="&nbsp;&nbsp;";
 $tplcontent['templates'].="$templatesetarray[$n]<br>";
 $templatelanguages=listdirs("themes/$templatesetarray[$n]");
 
 for($p=0;$p<count($templatelanguages);$p++){
  @chmod("templates/$templatesetarray[$n]/$templatelanguages[$p]",octdec(777));
  $tplcontent['templates'].="&nbsp;&nbsp;&nbsp;&nbsp;";
  $tplcontent['templates'].="$templatelanguages[$p]";
  $tplcontent['templates'].=" | <a href=\"admin_themes.php?deletefolder=templates/$templatesetarray[$n]/$templatelanguages[$p]\">Delete template set</a>";
  $tplcontent['templates'].=" | <a href=\"admin_themes.php?copyfolder=templates/$templatesetarray[$n]/$templatelanguages[$p]\">Copy template set</a>";
  $tplcontent['templates'].=" | <a href=\"admin_themes.php?renamefolder=templates/$templatesetarray[$n]/$templatelanguages[$p]\">Rename template set</a><br>";
  $templatearray=listfilesext("templates/$templatesetarray[$n]/$templatelanguages[$p]");
  
  for($m=0;$m<count($templatearray);$m++){
   $tplcontent['templates'].="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
   $tplcontent['templates'].="<a href=\"admin_themes.php?editfile=templates/$templatesetarray[$n]/$templatelanguages[$p]/$templatearray[$m]\">$templatearray[$m]</a><br>";
  }

 }
}

$replacementsetarray=listdirs("replacements");

for($n=0;$n<count($replacementsetarray);$n++){
 $tplcontent['replacements'].="&nbsp;&nbsp;";
 $tplcontent['replacements'].="$replacementsetarray[$n]";
 $tplcontent['replacements'].=" | <a href=\"admin_themes.php?deletefolder=replacements/$replacementsetarray[$n]\">Delete</a>";
 $tplcontent['replacements'].=" | <a href=\"admin_themes.php?copyfolder=replacements/$replacementsetarray[$n]\">Copy</a>";
 $tplcontent['replacements'].=" | <a href=\"admin_themes.php?renamefolder=replacements/$replacementsetarray[$n]\">Rename</a><br>";
 @chmod("replacements/$replacementsetarray[$n]",octdec(777));
 
 $replacementlanguages=listdirs("themes/$replacementsetarray[$n]");
 
 for($p=0;$p<count($replacementlanguages);$p++){
  $tplcontent['replacements'].="&nbsp;&nbsp;&nbsp;&nbsp;";
  $tplcontent['replacements'].="$replacementlanguages[$p]";
  @chmod("templates/$replacementsetarray[$n]/$replacementlanguages[$p]",octdec(777));
  $tplcontent['replacements'].=" | <a href=\"admin_themes.php?deletefolder=replacements/$replacementsetarray[$n]/$replacementlanguages[$p]\">Delete</a>";
  $tplcontent['replacements'].=" | <a href=\"admin_themes.php?copyfolder=replacements/$replacementsetarray[$n]/$replacementlanguages[$p]\">Copy</a>";
  $tplcontent['replacements'].=" | <a href=\"admin_themes.php?renamefolder=replacements/$replacementsetarray[$n]/$replacementlanguages[$p]\">Rename</a><br>";
  $replacementarray=listfilesext("replacements/$replacementsetarray[$n]/$replacementlanguages[$p]");
 
  for($m=0;$m<count($replacementarray);$m++){
   $tplcontent['replacements'].="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
   $tplcontent['replacements'].="<a href=\"admin_themes.php?editfile=replacements/$replacementsetarray[$n]/$replacementlanguages[$p]/$replacementarray[$m]\">$replacementarray[$m]</a><br>";
  }
 
 }

}

echo template('adminthemesview',$tplcontent);
}//nothing to do check

if($editfile){
	unset($tplcontent);
$filearray=file($editfile);

 $tplcontent['filename'].="Editing file: $editfile";

 $tplcontent['formheader'].="<form action=\"admin_themes.php\" method=post>";
 $tplcontent['formheader'].="<input type=hidden name=\"editfile2\" value=\"$editfile\" size=40>";

 $tplcontent['file'].="<textarea name=\"filecontents\" cols=90 rows=30 class=\"forminput\">";
 for($n=0;$n<count($filearray);$n++){
 $tplcontent['file'].=htmlentities($filearray[$n]);
 }
 $tplcontent['file'].="</textarea>";

 $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Update\" class=\"formbutton\">";
 $tplcontent['formfooter'].="</form>";
 
 echo template('adminthemesedit',$tplcontent);

 unset($editfile);

}

if($editfile2){

 $filearray=explode("\n",$filecontents);
 @unlink("$editfile2");

 $handle=fopen($editfile2,"a+");
 fwrite($handle,stripslashes("<?php die(); ?>\n"));
 for($n=0;$n<count($filearray);$n++){
  fwrite($handle,stripslashes($filearray[$n]));
 }
 fclose($handle);

 tableheader1();
 print "<tr>";
 print "<td class=\"tablecell1\">";
 print "<span class=\"textlarge\">";
 print "File '$editfile2' updated!<br>";
 print "</span>";
 print "</td>";
 print "</tr>";
 print "</table>";
 unset($editfile2);

}

if($renamefolder){
 unset($tplcontent);
 
 $tplcontent['formheader'].="<form action=\"admin_themes.php\" method=post>";

 $tplcontent['formheader'].="<input type=hidden name=\"renamefolder2\" value=\"$renamefolder\">";

 $tplcontent['oldname'].="Rename '$renamefolder'";
 $tplcontent['newname'].="<input type=text name=\"newname\" size=40 class=\"forminput\">";

 $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Rename\" class=\"formbutton\">";

 $tplcontent['formfooter'].="</form>";
 
 echo template('adminthemesrename',$tplcontent);
}

if($renamefolder2){
 $basefolder=substr($renamefolder2,0,strrpos($renamefolder2,"/")+1);
 rename($renamefolder2,"$basefolder"."$newname");

 tableheader1();
 print "<tr>";
 print "<td class=\"tablecell1\">";
 print "<span class=\"textlarge\">";
 print "'$renamefolder2' renamed!<br>";
 print "</span>";
 print "</td>";
 print "</tr>";
 print "</table>";
}

if($copyfolder){
 tableheader1();
 print "<tr>";
 print "<td class=\"tablecell1\">";

 print "<form action=\"admin_themes.php\" method=post>";
 print "<span class=\"textlarge\">";
 print "<input type=hidden name=\"copyfolder2\" value=\"$copyfolder\">";

 print "Copy '$copyfolder' to new folder named:<br>";
 print "<input type=text name=\"copyname\" size=40 class=\"forminput\"><br>";

 print "<br>";
 print "<input type=submit name=\"submit\" value=\"Rename\" class=\"formbutton\">";
 print "</span>";
 print "</td>";
 print "</form>";
 print "</tr>";
 print "</table>";
}

if($copyfolder2){
 $basefolder=substr($copyfolder2,0,strrpos($copyfolder2,"/")+1);
 $newpath="$basefolder"."$copyname";
 copydir($copyfolder2,$newpath);
 
 tableheader1();
 print "<tr>";
 print "<td class=\"tablecell1\">";
 print "<span class=\"textlarge\">";
 print "'$copyfolder2' copied to '$copyname'!<br>";
 print "</span>";
 print "</td>";
 print "</tr>";
 print "</table>";
}

if($deletefolder){
 deletedir("$deletefolder");

 tableheader1();
 print "<tr>";
 print "<td class=\"tablecell1\">";
 print "<span class=\"textlarge\">";
 print "'$deletefolder' deleted!<br>";
 print "</span>";
 print "</td>";
 print "</tr>";
 print "</table>";
}

}//admin check bracket

include("admin_footer.php");

include("footer.php");

?>
